package com.canbot.u05.activity.vipguide;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.bean.VipPersonMapData;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.utils.VolleyUtil;
import com.canbot.u05.widget.LoadingDialogUtils;
import com.canbot.u05.widget.RoundImageView;

import java.util.HashMap;

/**
 * Created by cwf on 2017/5/26.
 * 认识贵宾确认页面
 */

public class VipGuideConfirmActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private ImageView ivback;

        private Button ivok;

        private RoundImageView ivvipavatar;

        private TextView tvvipName;

        private String newVipName;

        private String vipFacePath;

        private Dialog dialogLoading;

        private String isSetting;

        private String identification;



        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_vip_confirm);
                vipFacePath = getIntent().getStringExtra("vipfacepath");
                newVipName=getIntent().getStringExtra("newvipname");
                isSetting=getIntent().getStringExtra("isSetting");
                identification=getIntent().getStringExtra("identification");
                setViews();
        }



        private void setViews() {
                ivback = (ImageView) findViewById(R.id.iv_back);
                ivok = (Button) findViewById(R.id.ok_btn);
                ivvipavatar = (RoundImageView) findViewById(R.id.iv_vip_avatar);
                tvvipName = (TextView) findViewById(R.id.tv_vip_name);
                if(TextUtils.isEmpty(isSetting)){
                        VolleyUtil.requestImage(IpMsgConst.HTTP_URL_CHEST + "/temp/faces/" + vipFacePath, ivvipavatar, R.drawable.sales_banner01, R.drawable.sales_banner01);

                }else{
                        VolleyUtil.requestImage(IpMsgConst.HTTP_URL_CHEST + "/temp/faces/" + vipFacePath, ivvipavatar, R.drawable.sales_banner01, R.drawable.sales_banner01);
                }
                tvvipName.setText(newVipName);
                ivback.setOnClickListener(this);
                ivok.setOnClickListener(this);
                if(TextUtils.isEmpty(isSetting)){
                        findViewById(R.id.text_vip_ok).setVisibility(View.GONE);
                }
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.iv_back:
                                finish();
                                break;
                        case R.id.ok_btn:
                                if(TextUtils.isEmpty(isSetting)){
                                        update();
                                }else{
                                        addRoute();
                                }
                                break;
                        default:
                                break;
                }
        }

        /**
         *修改贵宾名称
         * */
        public  void update() {

                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getResources().getString(R.string.request));
                HashMap<String, String> paramsMap = new HashMap<>();
                paramsMap.put("identification", identification);
                paramsMap.put("name", newVipName);
                paramsMap.put("vipType", "updateRoute");

                OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST2 + ":7755/" + "routevipGuide", paramsMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                MainApplication.finishActivity(SetVipNameActivity.class.getName());
                                finish();
                        }

                        @Override
                        public void onFailure() {
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                cancelDialog();
                                                ToastTools.newInstance(VipGuideConfirmActivity.this).showText(getString(R.string.network_error));
                                        }
                                });
                        }
                });


        }

        /**
         *添加贵宾信息
         * */
        private void addRoute(){

                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getResources().getString(R.string.request));
                String data=JSON.toJSONString(new VipPersonMapData(getIntent().getStringExtra("identification"),newVipName,vipFacePath));
                String url = "http://" + IpMsgConst.SOCKET_HOST2 + ":7755/routevipGuide";
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("vipType", "addRoute");
                hashMap.put("data",data );

                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Logger.e("OkHttpUtils", "onSuccess:" + result);
                                final JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                Logger.e("OkHttpUtils", "onSuccess:" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "strMsgBean.getRc()" + strMsgBean.getRc());
                                if (strMsgBean.getRc() >= 0) {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        cancelDialog();
                                                        startActivity(new Intent(VipGuideConfirmActivity.this,WelcomeSettingActivity.class)
                                                                .putExtra("isSetting", isSetting)
                                                                .putExtra("identification", identification)//id
                                                                .putExtra("vipname", newVipName));

                                                }
                                        });
                                }else{

                                        if(strMsgBean.getRc()==-2){

                                                runOnUiThread(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                                cancelDialog();
                                                                ToastTools.newInstance(VipGuideConfirmActivity.this).showText(strMsgBean.getResult().toString());


                                                        }
                                                });

                                        }
                                }
                        }

                        @Override
                        public void onFailure() {
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                cancelDialog();
                                                ToastTools.newInstance(VipGuideConfirmActivity.this).showText(getString(R.string.network_request_failed));
                                        }
                                });
                        }
                });
        }

        /**
         * 取消加载的Dialog
         */
        private void cancelDialog() {
                if (dialogLoading != null) {
                        if (dialogLoading.isShowing()) {
                                dialogLoading.cancel();
                        }
                }
        }

        @Override
        protected void onPause() {
                super.onPause();
                cancelDialog();
        }
}
